-- 英灵传说

local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local AbsActDefine = require("absAct.AbsActDefine")
local AbsActLogic = require("absAct.AbsActLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local HeroGrid = require("hero.HeroGrid")
local PanelDefine = require("broadcast.PanelDefine")
local HeroDefine = require("hero.HeroDefine")

function query(human, id)
    local absConfig = AbsActExcel.absActivity[id]
    local config = AbsActExcel.heroLoginPick[absConfig.actId]
    if not config then return end
    AbsActLogic.checkAbsActClean(human, id)
    local absAct =  human.db.absAct[id]
    
    local msgRet = Msg.gc.GC_ABS_ACT_HERO_LOGIN_PICK_QUERY
    msgRet.openDay = Util.diffDay(absConfig.realStartTime) + 1
    local heroCnt = 0
    for k, v in ipairs(config.items) do
        
        Grid.makeItem(msgRet.items[k], v[1], v[2])
        if HeroDefine.isHero(v[1]) then        
            heroCnt = heroCnt + 1    
            HeroGrid.makeHeroSimpleByID(msgRet.heroSimple[heroCnt], v[1])
        end
    end
    msgRet.itemGet = absAct.itemGet and 1 or 0
    msgRet.get[0] = 0
    for k, v in ipairs(config.loginReward) do
        Grid.makeItem(msgRet.login[k], v[1], v[2])
        if absAct[k] then
            msgRet.get[0] = msgRet.get[0] + 1 
            msgRet.get[msgRet.get[0]] = k
        end
    end
    msgRet.heroSimple[0] = heroCnt
    msgRet.items[0] = #config.items
    msgRet.login[0] = #config.loginReward
    Msg.send(msgRet,human.fd)
end


function get(human, type, id, param)
    local state= AbsActLogic.isStarted(human,  id)
    if not state then return end
    AbsActLogic.checkAbsActClean(human, id)
    local absAct =  human.db.absAct[id]
    local absConfig = AbsActExcel.absActivity[id]
    local config = AbsActExcel.heroLoginPick[absConfig.actId]
    if not config then return end

    local day = Util.diffDay(absConfig.realStartTime) + 1
    if type == 0 then
        day = day > #config.loginReward and #config.loginReward or day
        
        BagLogic.cleanMomentItemList()
        local len = 0
        for i = 1, day do
           if not absAct[i] then
              absAct[i] = 1
              len = len + 1
              BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, config.loginReward[i][1], config.loginReward[i][2])
           end
        end

        if len > 0 then
           BagLogic.addMomentItemList(human, "abs_loginpick")
        end
    else
        if day < #config.loginReward then
            return 
        end
        if absAct.itemGet then return end

        absAct.itemGet = 1
        BagLogic.cleanMomentItemList()
        BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, config.items[param][1], config.items[param][2])
        BagLogic.addMomentItemList(human, "abs_loginpick")
    end

    query(human, id)

    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, PanelDefine.PANEL_ID_3708)
end


function isRed(human, YYInfo, funcConfig)
    local state = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end
    local absConfig = AbsActExcel.absActivity[funcConfig.funcID]
    local config = AbsActExcel.heroLoginPick[absConfig.actId]
    local absAct = human.db.absAct[funcConfig.funcID]
    if not absAct then  return true end
    
    local day = Util.diffDay(absConfig.realStartTime) + 1
    day = day > #config.loginReward and #config.loginReward or day
    for i = 1, day do
        if not absAct[i] then
            return true
        end
    end

    if day >= #config.loginReward then
        if not absAct.itemGet then 
            return true
        end

    end
end


function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end